import request from "../../utils/request";
import { showToast, valIsEmpty } from "../../utils"
import store from "../../store";
import { userActionType } from "../../store/modules/user";

export function bindPhone(code) {
  return new Promise(async (resolve, reject) => {
    try {
      //用户拒绝授权获取手机号码或者获取手机号码
      if (!code) {
        reject();
        return;
      }
      uni.showLoading({
        title: "加载中",
      });
      //发送请求绑定手机号码
      await request({
        url: "userinfo/bindMemberPhone",
        data: {
          code: code,
        },
      });
      //刷新用户信息
      await store.dispatch(userActionType.action.refreshUserInfo)
      uni.hideLoading();
      resolve();
    } catch (e) {
      uni.hideLoading();
      console.log(e, "error");
      showToast.error(e);
      reject();
    }
  });
}
